﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.IO;
using System.Web.Script.Serialization;

namespace JogoUno
{

    /// <summary>
    /// Interaction logic for Menu.xaml
    /// </summary>
    public partial class Menu : Window
    {

        public Menu()
        {
            InitializeComponent();

            preencherSala();
        }

        public void preencherSala()
        {
            List<String> salas = new List<String>();
            string resposta = MainWindow.enviarMensagem("http://" + MainWindow.servidor + ":9000/uno/salas", "");

            if (resposta != null)
            {

                JavaScriptSerializer json = new JavaScriptSerializer();
                salas = json.Deserialize<List<String>>(resposta);

                lbox_salas.ItemsSource = null;
                lbox_salas.ItemsSource = salas;
            }
            /*
            MainWindow.writer.WriteLine("salas");

          
            String[] resposta = MainWindow.reader.ReadLine().Split('|');

            if (resposta[0] == "OK")
            {

                for (int i = 1; i < resposta.Length; i++)
                {
                    jogadores.Add(resposta[i]);
                }

                lbox_salas.ItemsSource = null;
                lbox_salas.ItemsSource = jogadores;
            }
            else
            {
                MessageBox.Show(this, resposta[1]);
            }

            */
        }

        //botao criar sala
        private void Button_Click(object sender, RoutedEventArgs e)
        {

            CriarSala cs = (CriarSala) MainWindow.exibirJanela(MainWindow.JANELA_CRIAR_SALA); //new CriarSala(networkStream, reader, writer, jogador);
            this.Hide();
            cs.Show();
            
        }

        //botao entrar sala
        private void button2_Click(object sender, RoutedEventArgs e)
        {

            String nomeSala = (String)lbox_salas.SelectedValue;
            if (nomeSala != null) {

                try
                {

                    List<String> salas = new List<String>();
                    string resposta = MainWindow.enviarMensagem("http://" + MainWindow.servidor + ":9000/uno/sala/"+ MainWindow.jogador.idJogador +"/"+ nomeSala, "");

                    if (resposta != null)
                    {
                        MainWindow.nomeSala = nomeSala;
                        Sala sala = (Sala)MainWindow.exibirJanela(MainWindow.JANELA_SALA);
                        this.Hide();
                        sala.Show();
                    }

/*
                    MainWindow.writer.WriteLine("entrarsala" + MainWindow.separador + nomeSala); //envia msg ao servidor
                    MainWindow.lider = false;
                    String[] resposta = MainWindow.reader.ReadLine().Split('|');

                    if (resposta[0] == "OK")
                    {
                        MainWindow.nomeSala = nomeSala;
                        Sala sala = (Sala)MainWindow.exibirJanela(MainWindow.JANELA_SALA);
                        this.Hide();
                        sala.Show();
                    }
                    else
                    {
                        MessageBox.Show(this, resposta[1]);
                    }
 */
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Erro: " + ex.Message);
                }
            } else {
                MessageBox.Show(this, "Antes de entrar selecione uma sala");
            }

        }

        private void button3_Click(object sender, RoutedEventArgs e)
        {
            MainWindow a = new MainWindow();
            this.Hide();
            a.Show();
        }

        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            MainWindow menu = (MainWindow)MainWindow.exibirJanela(MainWindow.JANELA_MAIN);
            menu.Show();
            MainWindow.fechar(MainWindow.JANELA_MENU);
        }

        private void btn_atualizar_Click(object sender, RoutedEventArgs e)
        {
            preencherSala();
        }

    }
}
